package com.neusoft.hifly.lock;

import java.util.concurrent.atomic.AtomicInteger;

// 使用Atomic包
public class RushOrderAtomic {
	// 库存6万
	AtomicInteger i = new AtomicInteger(60000);

	public void order() { // 存放在虚拟机栈中
		i.decrementAndGet(); // 这个操作是原子性的
	}

	public static void main(String[] args) throws Exception {
		RushOrderAtomic rushOrder = new RushOrderAtomic();
		for (int j = 0; j < 6; j++) {
			new Thread(() -> {
				for (int k = 0; k < 10000; k++) {
					rushOrder.order();
				}
			}).start();
		}
		Thread.sleep(5000);
		System.out.println("库存量：" + rushOrder.i); // 结果值为什么不是0?
	}

}